package com.etl.user.service;

import com.etl.sms.enums.SmsType;
import com.etl.user.model.User;
import com.etl.user.dao.IUserDao;
import com.xr.redis.util.CacheManager;
import com.xr.service.impl.BaseServiceImpl;
import com.xr.util.Assert;
import com.xr.util.Utils;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-03-25
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<IUserDao, User> implements IUserService {

  @Override
  public void signup(User newUser) throws Exception {
    Assert.hasLength(newUser.getMobileNumber(), "手机号为空");
    Assert.isNumber(newUser.getMobileNumber(), "手机号不合法");

    User existedUser = this.selectOne(this.buildWhereWrapper(Utils.newHashMap("mobile_number", newUser.getMobileNumber())));
    Assert.isNull(existedUser, "手机号已经注册，不能重复注册。");

    int code = Utils.randomNumber(6);
    // TODO 发送短信

    CacheManager.getCacheService().set(SmsType.SIGNUP_VERIFY_CODE.name()+"."+newUser.getMobileNumber(), code, 10, TimeUnit.MINUTES);

  }

  @Override
  public User signin(String mobileNumber) throws Exception {
    return null;
  }

  @Override
  public void signout(String mobileNumber) throws Exception {

  }
}
